﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.IO;

namespace LPNRTool.ParkLock
{
    class ReadJson
    {
        //"result":true,
        //"msg":"车位锁下降成功"
        //LockMain lockmain = new LockMain();
        WriteLog trace = new WriteLog();

        //查询的字段
        public string ReceJsonStr = "";
        public string result = "";
        public string msg = "";
        public string slaveId = "";
        public string openState = "";
        public string carState = "";
        public string voltage = "";
        public string equiState = "";

        public struct SignalJsonToMy
        {
            public string result { get; set; }
            public string msg { get; set; }
        }

        public void SignalJsonMy(string str)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            SignalJsonToMy list = js.Deserialize<SignalJsonToMy>(str);
            string result = list.result;
            string msg = list.msg;
            string jsonstr = result + "&" + msg;
            //MessageBox.Show("SignalJsonMy处理格式化的Json数据" + result + msg);
            ReceJsonHandle(jsonstr);
        }

        public void ReceJsonHandle(string str)
        {
            int count = 0;
            //ReceJsonStr = string.Format("{0:yy-MM-dd hh:mm:ss:fff} {1}", DateTime.Now, "收到的Json数据:" + str + "\r\n");
            ReceJsonStr = string.Format("收到的Json数据:" + str);
            trace.WriteLogFile(ReceJsonStr);
            List<string> list = new List<string>(str.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries));
            count = list.Count;
          
            //操作升降车锁返回值
            if (count < 3)
            {
                foreach (string t in list)
                {
                    result = list[0];
                    msg = list[1];
                    break;
                }

                if (LockMain.ControlLockBit == 1)//操作车锁升降
                {
                    if (result == "True")
                    {
                        LockMain._isRiseFallExit = true;
                        //MessageBox.Show(msg, "操作锁成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        LockMain._isRiseFallExit = true;
                        //MessageBox.Show(msg, "操作锁失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        //{
        //"result":false,
        //"msg":"数据获取成功",
        //"data":
        //{
        //"slaveId":"000745900745",
        //"openState":1,
        //"carState":0,
        //"voltage":5.75,
        //"equiState":0
        //}
        //}
        public struct ImpactionJsonToMy
        {
            public string result { get; set; }
            public string msg { get; set; }
            public ImpactionJsonData data;
        }
        public struct ImpactionJsonData
        {
            public string slaveId { get; set; }
            public int openState { get; set; }
            public int carState { get; set; }
            public float voltage { get; set; }
            public int equiState { get; set; }
        }

        public void ImpactionJsonMy(string str)
        {
           JavaScriptSerializer js = new JavaScriptSerializer();
            ImpactionJsonToMy list = js.Deserialize<ImpactionJsonToMy>(str);
            string result = list.result;
            string msg = list.msg;
            string slaveId = list.data.slaveId;
            int openState = list.data.openState;
            int carState = list.data.carState;
            float voltage = list.data.voltage;
            int equiState = list.data.equiState;
            string jsonstr = result + "&" + msg + "&" + slaveId + "&" + Convert.ToString(openState) + "&" + Convert.ToString(carState) + "&" + voltage + "&" + Convert.ToString(equiState);

            //JsonSendLog("\r\n" + "result: " + result + "\r\n" + "msg: " + msg + "\r\n" + "slaveId: " + slaveId + "\r\n" + "openState: " + Convert.ToString(openState) + "\r\n" + "carState: " + Convert.ToString(carState) + "\r\n" + "voltage: " + voltage + "\r\n" + "equiState: " + Convert.ToString(equiState) + "\r\n");
        }

    }
}
